package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysContract;
import com.ruoyi.system.domain.SysStaff;
import com.ruoyi.system.domain.request.SysStaffRequest;
import com.ruoyi.system.service.ISysContractService;
import com.ruoyi.system.service.ISysStaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

/**
 * 教职员工Controller
 *
 * @author ruoyi
 * @date 2024-11-14
 */
@RestController
@RequestMapping("/system/staff")
public class SysStaffController extends BaseController {
    @Autowired
    private ISysStaffService sysStaffService;
    @Autowired
    private ISysContractService sysContractService;

    /**
     * 查询教职员工列表
     */
    @PreAuthorize("@ss.hasPermi('system:staff:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysStaffRequest sysStaffRequest) {
        startPage();
        List<SysStaff> list = sysStaffService.selectSysStaffList(sysStaffRequest);
        return getDataTable(list);
    }

    /**
     * 查询部门员工数量
     *
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:staff:list')")
    @GetMapping("/deptStaffCount")
    public AjaxResult deptStaffCount() {
        startPage();
        List<HashMap<String, Integer>> list = sysStaffService.queryDeptStaffCount();
        return AjaxResult.success(list);
    }

    /**
     * 导出教职员工列表
     */
    @PreAuthorize("@ss.hasPermi('system:staff:export')")
    @Log(title = "教职员工", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysStaffRequest sysStaffRequest) {
        List<SysStaff> list = sysStaffService.selectSysStaffList(sysStaffRequest);
        ExcelUtil<SysStaff> util = new ExcelUtil<SysStaff>(SysStaff.class);
        util.exportExcel(response, list, "教职员工数据");
    }

    /**
     * 导出教职员工列表
     */
    @PreAuthorize("@ss.hasPermi('system:staff:import')")
    @Log(title = "教职员工", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<SysStaff> util = new ExcelUtil<SysStaff>(SysStaff.class);
        List<SysStaff> sysStaffList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = sysStaffService.importData(sysStaffList, updateSupport, operName);
        return success(message);
    }

    /***
     *
     * 下载导入模板
     * @param response
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<SysStaff> util = new ExcelUtil<SysStaff>(SysStaff.class);
        util.importTemplateExcel(response, "教职工数据");
    }


    /**
     * 获取教职员工详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:staff:query')")
    @GetMapping(value = "/{staffId}")
    public AjaxResult getInfo(@PathVariable("staffId") Long staffId) {
        return success(sysStaffService.selectSysStaffByStaffId(staffId));
    }

    /**
     * 新增教职员工
     */
    @PreAuthorize("@ss.hasPermi('system:staff:add')")
    @Log(title = "教职员工", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysStaff sysStaff) {
        return toAjax(sysStaffService.insertSysStaff(sysStaff));
    }

    /**
     * 修改教职员工
     */
    @PreAuthorize("@ss.hasPermi('system:staff:edit')")
    @Log(title = "教职员工", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysStaff sysStaff) {
        return toAjax(sysStaffService.updateSysStaff(sysStaff));
    }

    /**
     * 删除教职员工
     */
    @PreAuthorize("@ss.hasPermi('system:staff:remove')")
    @Log(title = "教职员工", businessType = BusinessType.DELETE)
    @DeleteMapping("/{staffIds}")
    public AjaxResult remove(@PathVariable Long[] staffIds) {
        //先判断名下是否拥有合同附件，有则不能删除
        for (Long staffId : staffIds) {
            LambdaQueryWrapper<SysContract> queryWrapper = new LambdaQueryWrapper<SysContract>()
                    .eq(SysContract::getStaffId, staffId);
            List<SysContract> contractList = this.sysContractService.list(queryWrapper);
            if (contractList.size() > 0) {
                return AjaxResult.error("员工编号【" + staffId + "】存在合同，不能删除!");
            }
        }
        return toAjax(sysStaffService.deleteSysStaffByStaffIds(staffIds));
    }
}
